<?php 
class ControllerSearch extends Controller { 	
	function index() { 
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');	

		//pagination
        $session->set('search.page', $request->has('page')?(int)$request->get('page'):1);

    	$language->load('controller/search.php');
	
		$template->set('title', $language->get('heading_title'));
	  	  
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));
  
    	$view->set('text_critea', $language->get('text_critea'));
    	$view->set('text_search', $language->get('text_search'));
		$view->set('text_keywords', $language->get('text_keywords'));
		$view->set('text_error', $language->get('text_error'));
	 
		$view->set('entry_search', $language->get('entry_search'));
    	$view->set('entry_description', $language->get('entry_description'));
    	$view->set('entry_page', $language->get('entry_page'));
		  
    	$view->set('button_search', $language->get('button_search'));
    
    	$view->set('action', $url->href('search', 'page'));
  
		$view->set('search', $session->get('search.search'));
	
		$view->set('description', $session->get('search.description'));
	
		if ($session->get('search.search')) {
      		if ($session->get('search.description')) {
				$sql = "select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where pd.language_id = '?' and (pd.name like '?' or pd.description like '?') and p.date_available < now() and p.status = '1'";
				$results = $database->getRows($database->splitQuery($database->parse($sql, $language->getId(), '%' . $session->get('search.search') . '%', '%' . $session->get('search.search') . '%'), $session->get('search.page'), $config->get('config_max_rows')));
      		} else {
				$sql = "select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where pd.language_id = '?' and pd.name like '?' and p.date_available < now() and p.status = '1'";
				$results = $database->getRows($database->splitQuery($database->parse($sql, $language->getId(), '%' . $session->get('search.search') . '%'), $session->get('search.page'), $config->get('config_max_rows')));
			}
			
			if ($results) {
				$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));
		        
        		$product_data = array();

        		foreach ($results as $result) {
          			$product_data[] = array(
            			'name'  => $result['name'],
            			'href'  => $url->href('product', FALSE, array('product_id' => $result['product_id'])),
            			'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
            			'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax')))
          			);
        		}

       			$view->set('products', $product_data);

 		       	$view->set('page', $session->get('search.page'));
  
        		$page_data = array();

        		for ($i = 1; $i <= $database->getPages(); $i++) {
					$query=array('path' => $request->gethtml('path'));
					if ($i > 1) $query['page'] = $i;
        			$page_data[] = array(
          				'text'  => $language->get('text_pages', $i, $database->getPages()),
						'href'	=> $url->href('search', FALSE, $query),
          				'value' => $i
        			);
        		}
  
        		$view->set('pages', $page_data);
			}
		}

		$template->set('content', $view->fetch('content/search.tpl'));

		$template->set('content', $view->fetch('common/grid.tpl'), TRUE);
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
	

	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');

		$query=array();
	
		if ($request->has('search', 'post')) {
			$session->set('search.search', $request->gethtml('search', 'post'));
	  	}
	  	
		if ($request->has('description', 'post')) {
			$session->set('search.description', $request->gethtml('description', 'post'));
	  	} else {
			$session->delete('search.description');
		}

		//backwards compat pagination
      	if (($request->has('page', 'post')) || ($request->has('search', 'post'))) {
			$i=(int)$request->get('page', 'post');
			if ($i > 1) $query['page'] = $i;
      	}
		
		$response->redirect($url->href('search', FALSE, $query));
	}
}
?>
